import { _decorator, Component, Node, Vec3, RigidBody2D, Vec2 ,Sprite} from 'cc';
const { ccclass, property } = _decorator;

@ccclass('GravityWell')
export class GravityWell extends Component {
    
    @property
    gravityStrength: number = 1000000;  // 引力强度
    
    @property
    maxRadius: number = 150;  // 最大引力作用半径
    
    @property(Node)
    rangeIndicator: Node = null!;  // 范围指示器节点
    
    onLoad() {
        // 初始化范围指示器
        if (this.rangeIndicator) {
            this.rangeIndicator.setScale(this.maxRadius / 150, this.maxRadius / 150);
            
            // 添加呼吸效果
            this.addBreathingEffect();
        }
    }
    
    // 计算对目标位置的引力
    calculateGravity(targetPos: Vec3): Vec2 {
        const wellPos = this.node.getPosition();
        
        // 计算距离向量 (从目标指向引力井中心)
        const distanceVecX = wellPos.x - targetPos.x;
        const distanceVecY = wellPos.y - targetPos.y;
        const distance = Math.sqrt(distanceVecX * distanceVecX + distanceVecY * distanceVecY);
        
        
        // 超出范围或距离太近时返回零向量
        if (distance > this.maxRadius || distance < 20) {
            return new Vec2(0, 0);
        }
        
        // 计算引力大小（距离越近引力越大）
        const normalizedDistance = distance / this.maxRadius;
        const force = (1 - normalizedDistance * normalizedDistance) * this.gravityStrength;
        
        // 计算方向（指向引力井中心）
        const directionX = distanceVecX / distance;  // 单位向量
        const directionY = distanceVecY / distance;
        
        const resultForce = new Vec2(directionX * force, directionY * force);
        
        return resultForce;
    }
    
    // 添加呼吸效果
    addBreathingEffect() {
        if (!this.rangeIndicator) return;
        
        const originalScale = this.rangeIndicator.getScale();
        
        // 创建缩放动画
        const scaleUp = () => {
            this.rangeIndicator.setScale(originalScale.x * 1.05, originalScale.y * 1.05);
        };
        
        const scaleDown = () => {
            this.rangeIndicator.setScale(originalScale.x * 0.95, originalScale.y * 0.95);
        };
        
        // 循环执行呼吸效果
        let breathingCallback: Function;
        breathingCallback = () => {
            scaleUp();
            this.scheduleOnce(scaleDown, 0.8);
        };
        
        this.schedule(breathingCallback, 1.6);
    }
    
    // 激活效果
    activate() {
        this.node.setScale(0.8, 0.8);
        
        // 放大出现动画
        const targetScale = 1.0;
        const duration = 0.2;
        
        // 简单的缩放动画
        let currentTime = 0;
        const originalScale = 0.8;
        
        let scaleCallback: Function;
        scaleCallback = () => {
            currentTime += 0.016; // 约60fps
            const progress = Math.min(currentTime / duration, 1);
            const currentScale = originalScale + (targetScale - originalScale) * progress;
            
            this.node.setScale(currentScale, currentScale);
            
            if (progress >= 1) {
                this.unschedule(scaleCallback);
            }
        };
        
        this.schedule(scaleCallback, 0.016);
    }
    
    // 销毁前的动画效果
    deactivate() {
        const duration = 0.3;
        let currentTime = 0;
        const originalScale = this.node.getScale().x;
        
        let fadeCallback: Function;
        fadeCallback = () => {
            currentTime += 0.016;
            const progress = Math.min(currentTime / duration, 1);
            const currentScale = originalScale * (1 - progress);
            const alpha = 255 * (1 - progress);
            
            this.node.setScale(currentScale, currentScale);
            // 设置透明度（如果有Sprite组件）
            const sprite = this.node.getComponent(Sprite);
            if (sprite) {
                 sprite.color = sprite.color.set(sprite.color.r,sprite.color.g,sprite.color.b,alpha);
            }
            
            if (progress >= 1) {
                this.unschedule(fadeCallback);
                this.node.destroy();
            }
        };
        
        this.schedule(fadeCallback, 0.016);
    }
}